<template>
  <div class="singer">
    <listview :data="singlist" @select="selectSinger"></listview>
    <router-view></router-view>
  </div>
</template>
<script>
  import listview from "@/base/listview"
  import {mapMutations} from "vuex"
  import {
    getSingerList
  } from "@/api/singer"
  import {
    ERR_OK
  } from "@/api/config"
  import Singer from "@/assets/js/singer"
  const HOT_NAME = "热门数据"
  const HOT_SINGER_LEN = 10
  export default {
    name: "singer",
    data() {
      return {
        singlist: [],
        ss:"haha"
      };
    },
    methods: {
      ...mapMutations({
        setSinger:'SET_SINGER'
      }),
      async _getSingerList() {
        try {
          const res = await getSingerList()
          if (ERR_OK == res.code) {
            this.singlist = this._normalizeSinger(res.data.list)
          }
        } catch (error) {
          console.log(error)
        }
      },
      _normalizeSinger(list) {
        let map = {
          hot: {
            title: HOT_NAME,
            items: []
          }
        }
        list.forEach((item, index) => {
          if (index < HOT_SINGER_LEN) {
            map.hot.items.push(new Singer({
              id: item.Fsinger_id,
              name: item.Fsinger_name,
              mid: item.Fsinger_mid
            }))
          }
          const key = item.Findex
          if (!map[key]) {
            map[key] = {
              title: key,
              items: []
            }
          }
          map[key].items.push(new Singer({
            id: item.Fsinger_id,
            name: item.Fsinger_name,
            mid: item.Fsinger_mid
          }))
        })
        //处理map得到有序列表
        
        let hot = []
        let ret = []
        for (let key in map) {
          let val = map[key]
          if (val.title.match(/[a-zA-z]/)) {
            ret.push(val)
          } else if (val.title === HOT_NAME) {
            hot.push(val)
          }
        }
        ret.sort((a, b) => {
          return a.title.charCodeAt(0) - b.title.charCodeAt(0)
        })
        return hot.concat(ret)
      },
      selectSinger(singer){
        this.$router.push({
          path:'/singer/'+singer.mid
        })
        this.setSinger(singer)
      },
    },
    
    created() {
      this._getSingerList()
    },
    components: {
      listview
    }

  }

</script>
<style lang="scss" scoped>
  .singer {
    position: fixed;
    top: 104px;
    bottom: 0px;
    width: 100%;
    z-index: 99;
  }



</style>
